/*
 * 用户注册界面
 */
package com.wonyesoft.wuliu.acticity;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wonyesoft.wuliu.AppContent;
import com.wonyesoft.wuliu.R;
import com.wonyesoft.wuliu.acticity.base.BaseActivity;
import com.wonyesoft.wuliu.acticity.utils.HttpUtilsEx;
import com.wonyesoft.wuliu.acticity.utils.ToastUtils;
import com.wonyesoft.wuliu.api.GagApi;
import com.wonyesoft.wuliu.vo.JsonRequest;
import com.wonyesoft.wuliu.vo.JsonResponse;
import com.wonyesoft.wuliu.vo.State;

public class UserAddListActivity extends BaseActivity implements
		OnClickListener {

	private LinearLayout gonggao,gengxin,about_us,tousu;
	private TabHost mTabHost;
	private RadioGroup mRadioGroup;
	private TextView title, head_right;
	private ImageView head_left;
	private String orders_deliver,orders_deliverAdd,orders_deliverTell,orders_receipt,
	orders_receiptAdd,orders_receiptTell,orders_goodsName,orders_weight,
	orders_volume,orders_remark,orders_goodsType,cash,byesorno;
	
	private EditText orders_deliver_EditText,orders_deliverAdd_EditText,orders_deliverTell_EditText,orders_receipt_EditText,
	orders_receiptAdd_EditText,orders_receiptTell_EditText,orders_goodsName_EditText,orders_weight_EditText,
	orders_volume_EditText,orders_remark_EditText;
	
	private Spinner orders_goodsType_Spinner;
	
	private Button orders_payWay_cash,orders_payWay_delivery,orders_payWay_monthly,orders_payWay_other,orders_byes,orders_bno,
	orders_ture;
	
	private Gson gson = new Gson();
	private HttpUtils http = new HttpUtils();
	private RequestParams params;
	
	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.orders);
		inIds();
		title = (TextView) findViewById(R.id.head_title);
		head_left = (ImageView) findViewById(R.id.head_left);
		head_left.setVisibility(0);
		title.setText("用户下单");
		head_left.setOnClickListener(this);
	}

	private void inIds() {
		orders_deliver_EditText = (EditText) findViewById(R.id.orders_deliver_EditText);
		orders_deliverAdd_EditText = (EditText) findViewById(R.id.orders_deliverAdd_EditText);
		orders_deliverTell_EditText = (EditText) findViewById(R.id.orders_deliverTell_EditText);
		orders_receipt_EditText = (EditText) findViewById(R.id.orders_receipt_EditText);
		orders_receiptAdd_EditText = (EditText) findViewById(R.id.orders_receiptAdd_EditText);
		orders_receiptTell_EditText = (EditText) findViewById(R.id.orders_receiptTell_EditText);
		orders_goodsName_EditText = (EditText) findViewById(R.id.orders_goodsName_EditText);
		orders_weight_EditText = (EditText) findViewById(R.id.orders_weight_EditText);
		orders_volume_EditText = (EditText) findViewById(R.id.orders_volume_EditText);
		orders_remark_EditText = (EditText) findViewById(R.id.orders_remark_EditText);
		
		orders_goodsType_Spinner = (Spinner) findViewById(R.id.orders_goodsType_Spinner);
		
		orders_payWay_cash = (Button) findViewById(R.id.orders_payWay_cash);
		orders_payWay_delivery = (Button) findViewById(R.id.orders_payWay_delivery);
		orders_payWay_monthly = (Button) findViewById(R.id.orders_payWay_monthly);
		orders_payWay_other = (Button) findViewById(R.id.orders_payWay_other);
		orders_bno = (Button) findViewById(R.id.orders_bno);
		orders_byes = (Button) findViewById(R.id.orders_byes);
		
		
		orders_ture = (Button) findViewById(R.id.orders_ture);
		orders_payWay_cash.setOnClickListener(this);
		orders_payWay_delivery.setOnClickListener(this);
		orders_payWay_monthly.setOnClickListener(this);
		orders_payWay_other.setOnClickListener(this);
		orders_ture.setOnClickListener(this);		
	}
	
	
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.head_left:
			finish();
			break;
		case R.id.orders_payWay_cash:
			cash = 1 +"";
			finish();
			break;
		case R.id.orders_payWay_delivery:
			cash = 2 +"";
			finish();
			break;
		case R.id.orders_payWay_monthly:
			cash = 3 +"";
			finish();
			break;
		case R.id.orders_bno:
			byesorno = 1 +"";
			finish();
			break;
		
		case R.id.orders_byes:
			byesorno = 2 +"";
			finish();
			break;
			
		case R.id.orders_ture: 
			reg();
			break;
		default:
			break;
		}
	}

	private void reg(){
		orders_deliver = orders_deliver_EditText.getText().toString();
		orders_deliverAdd = orders_deliverAdd_EditText.getText().toString();
		orders_deliverTell = orders_deliverTell_EditText.getText().toString();
		orders_receipt = orders_receipt_EditText.getText().toString();
		orders_receiptAdd = orders_receiptAdd_EditText.getText().toString();
		orders_receiptTell = orders_receiptTell_EditText.getText().toString();
		orders_goodsName = orders_goodsName_EditText.getText().toString();
		orders_weight = orders_weight_EditText.getText().toString();
		orders_volume = orders_volume_EditText.getText().toString();
		orders_remark = orders_remark_EditText.getText().toString();
		
//		orders_goodsType = orders_goodsType_Spinner.getSelectedItem().toString();
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("sdCompany", orders_deliver);
		dataMap.put("sdAddress", orders_deliverAdd);
		dataMap.put("sdMobile", orders_deliverTell);
		dataMap.put("reCompany", orders_receipt);
		dataMap.put("reAddress", orders_receiptAdd);
		dataMap.put("reMobile", orders_receiptTell);
		dataMap.put("cargoName", orders_goodsName);
		dataMap.put("cargoType", "1");  //orders_goodsType
		
		dataMap.put("cargoWeight", orders_weight);
		dataMap.put("cargoVolume", orders_volume);
		dataMap.put("Remark", orders_remark);
		
		
		dataMap.put("payType", cash);
		dataMap.put("isNickBack", byesorno);
		
		JsonRequest jr = JsonRequest.getInit();
		jr.setData(dataMap);
		String json = gson.toJson(jr);
		params = new RequestParams();
		System.out.println(json);
		params.addBodyParameter("jsonParam", json);
		http.configCookieStore(AppContent.getPreferencesCookieStore());
		http.send(HttpRequest.HttpMethod.POST,GagApi.OnlineOrder, params,
				new RequestCallBack<String>() {


					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						Gson gson2 = new Gson();
						JsonResponse jrs = gson2.fromJson(
								responseInfo.result, JsonResponse.class);
						System.out.println(responseInfo.result);
						State state = jrs.getState();

						if (state.getCode().equals(State.SUCEECCODE)) {
							System.out.println(state.getMsg());
							openActivity(UserLoginActivity.class);
							finish();
						} else
							//ToastUtils.showLong(state.getMsg());
							
						System.out.println(state.getMsg());
						
						

					}

					@Override
					public void onFailure(HttpException error, String msg) {
						// testTextView.setText(error.getExceptionCode() +
						// ":" +
						// msg);
						System.out.println(msg);
					}
				});
		
	}
}
